package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.pmps.dao.POCourseSupplierDetailDao;
import com.huike.pmps.dao.SupplierDao;
import com.huike.pmps.dao.SupplierEvaluatesDao;
import com.huike.pmps.dao.SupplierSettlementDetailDao;
import com.huike.pmps.model.constants.GlobalConstants;
import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.*;
import com.huike.pmps.model.vo.PageResult;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.model.vo.supplier.SupplierServiceContentVo;
import com.huike.pmps.model.vo.supplier.SupplierVo;
import com.huike.pmps.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.Double.NaN;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Slf4j
@Service
public class SupplierEvaluateServiceImpl implements SupplierEvaluatesService {

    @Autowired
    private SupplierEvaluatesDao supplierEvaluatesDao;
    @Autowired
    POCourseSupplierDetailDao poCourseSupplierDetailDao;
    @Autowired
    private SupplierDao supplierDao;
    @Override
    public Result querySupplierEvaluatesByCourseId(Integer poCourseId) {
        QueryWrapper<POCourseSupplierDetail> queryWrapperSupplier = new QueryWrapper<>();
        queryWrapperSupplier.eq("po_course_id", poCourseId);
        List<POCourseSupplierDetail> courseList = poCourseSupplierDetailDao.selectList(queryWrapperSupplier);
        List<SupplierEvaluates> seList = new ArrayList<SupplierEvaluates>();
        for(POCourseSupplierDetail item : courseList){
            String serviceType = item.getServiceTypeName();
            Double finalEva = item.getFinalEvaluation();
            SupplierEvaluates se = new SupplierEvaluates();
            if(finalEva == null){
                se.setServiceType(serviceType);
                se.setSupplierName(item.getSupplierName());
                se.setPoId(item.getPoId());
                se.setSupplierId(item.getSupplierId());
                se.setCourseId(item.getCourseId());
                se.setPoCourseId(item.getPoCourseId());
                se.setCourseName(item.getCourseName());
                if(serviceType.equals("整包")){
                    se.setShootLevel(0);
                    se.setPostProduction(0);
                    se.setDelivery2Level(0);
                    se.setDelivery3Level(0);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else if(serviceType.equals("拍摄")){
                    se.setShootLevel(0);
                    se.setPostProduction(-1);
                    se.setDelivery2Level(-1);
                    se.setDelivery3Level(-1);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else if (serviceType.equals("制作")){
                    se.setShootLevel(-1);
                    se.setPostProduction(0);
                    se.setDelivery2Level(-1);
                    se.setDelivery3Level(-1);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else if (serviceType.equals("化妆") || serviceType.equals("租赁") ||serviceType.equals("配音") || serviceType.equals("翻译") || serviceType.equals("字幕")){
                    se.setShootLevel(-1);
                    se.setPostProduction(-1);
                    se.setDelivery2Level(-1);
                    se.setDelivery3Level(-1);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else if (serviceType.equals("动画（二维）")){
                    se.setShootLevel(0);
                    se.setPostProduction(0);
                    se.setDelivery2Level(-1);
                    se.setDelivery3Level(0);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else if (serviceType.equals("动画（三维）")){
                    se.setShootLevel(0);
                    se.setPostProduction(0);
                    se.setDelivery2Level(0);
                    se.setDelivery3Level(-1);
                    se.setServiceAttitude(0);
                    se.setDeliverySchedule(0);
                    se.setCommunicateSkill(0);
                    se.setAppraise("请填写评级");
                }else {
                    return Result.failure("服务参数错误");
                }
                seList.add(se);
            }else{
                QueryWrapper<SupplierEvaluates> queryWrapperSupplierEV = new QueryWrapper<SupplierEvaluates>();
                queryWrapperSupplierEV.eq("supplier_id", item.getSupplierId());
                queryWrapperSupplierEV.eq("po_id", item.getPoId());
                queryWrapperSupplier.eq("course_id",item.getCourseId());
                List<SupplierEvaluates> supplierEvaluates = supplierEvaluatesDao.selectList(queryWrapperSupplierEV);
                seList.addAll(supplierEvaluates);
            }
        }
        System.out.println(seList.toString());
        return Result.success(seList);
    }

    @Override
    public Result selectBySupplierId(Integer supplierTypeId) {
        QueryWrapper<SupplierEvaluates> queryWrapperSupplierEV = new QueryWrapper<SupplierEvaluates>();
        queryWrapperSupplierEV.eq("supplier_id", supplierTypeId);
        List<SupplierEvaluates> evList = supplierEvaluatesDao.selectList(queryWrapperSupplierEV);
        Double shootLevel = 0D;
        Double postProduction = 0D;
        Double delivery2Level = 0D;
        Double delivery3Level = 0D;
        Double serviceAttitude = 0D;
        Double deliverySchedule = 0D;
        Double communicateSkill = 0D;
        int shootLevelCount = 0;
        int postProductionCount = 0;
        int delivery2LevelCount = 0;
        int delivery3LevelCount = 0;
        int serviceAttitudeCount = 0;
        int deliveryScheduleCount = 0;
        int communicateSkillCount = 0;
        for(SupplierEvaluates se : evList){
            Integer shootLevelItem = se.getShootLevel();
            if(shootLevelItem != -1){
                shootLevel += shootLevelItem.doubleValue();
                shootLevelCount ++;
            }
            Integer postProductionItem = se.getPostProduction();
            if(postProductionItem != -1){
                postProduction += postProductionItem.doubleValue();
                postProductionCount ++;
            }
            Integer delivery2LevelItem = se.getDelivery2Level();
            if(delivery2LevelItem != -1){
                delivery2Level += delivery2LevelItem.doubleValue();
                delivery2LevelCount ++;
            }
            Integer delivery3LevelItem = se.getDelivery3Level();
            if(delivery3LevelItem != -1){
                delivery3Level += delivery3LevelItem.doubleValue();
                delivery3LevelCount ++;
            }
            Integer serviceAttitudeItem = se.getServiceAttitude();
            if(serviceAttitudeItem != -1){
                serviceAttitude += serviceAttitudeItem.doubleValue();
                serviceAttitudeCount ++;
            }
            Integer deliveryScheduleItem = se.getDeliverySchedule();
            if(deliveryScheduleItem != -1){
                deliverySchedule += deliveryScheduleItem.doubleValue();
                deliveryScheduleCount ++;
            }
            Integer communicateSkillItem = se.getCommunicateSkill();
            if(communicateSkillItem != -1){
                communicateSkill += communicateSkillItem.doubleValue();
                communicateSkillCount ++;
            }
        }
        shootLevel = shootLevel / (shootLevelCount == 0? 1: shootLevelCount);
        postProduction = postProduction /  (postProductionCount == 0? 1: postProductionCount); ;
        delivery2Level = delivery2Level /  (delivery2LevelCount == 0? 1: delivery2LevelCount);;
        delivery3Level =delivery3Level /  (delivery3LevelCount == 0? 1: delivery3LevelCount);;
        serviceAttitude =serviceAttitude /  (serviceAttitudeCount == 0? 1: serviceAttitudeCount);;
        deliverySchedule = deliverySchedule /  (deliveryScheduleCount == 0? 1: deliveryScheduleCount);;
        communicateSkill =communicateSkill /  (communicateSkillCount == 0? 1: communicateSkillCount);;
        Double evSum = (shootLevel + postProduction + delivery2Level + delivery3Level + serviceAttitude + deliverySchedule + communicateSkill)/ 7;
        Map data = new HashMap<>();
        System.out.println(evSum);
        data.put("shootLevel",shootLevel);
        data.put("postProduction", postProduction);
        data.put("delivery2Level",delivery2Level);
        data.put("delivery3Level",delivery3Level);
        data.put("serviceAttitude",serviceAttitude);
        data.put("deliverySchedule",deliverySchedule);
        data.put("communicateSkill",communicateSkill);
        data.put("finalEvaluation", evSum);
        data.put("dataList",evList);
        return Result.success(data);
    }
    /**
     * 1、将评价数据插入数据库；
     * 2、根据supplierId查询各供应商的评价数据进行统计；
     * 3、将统计后的数据更新值supplier表
     */

    @Override
    public Result batchUpdate(List<SupplierEvaluates> suppliersEvaluates) {
        //1、将评价数据插入数据库
        supplierEvaluatesDao.insertSupplierEvaluatesBatch(suppliersEvaluates);

        List<Integer> supplierIds = new ArrayList<Integer>();
        for(SupplierEvaluates item : suppliersEvaluates){
            supplierIds.add(item.getSupplierId());
        }

        //2、
        List<Supplier> suppliers = new ArrayList<Supplier>();
        for(Integer item : supplierIds){
            Supplier supplier = new Supplier();
            QueryWrapper<SupplierEvaluates> queryWrapperSupplierEV = new QueryWrapper<SupplierEvaluates>();
            queryWrapperSupplierEV.eq("supplier_id", item);
            List<SupplierEvaluates> evList = supplierEvaluatesDao.selectList(queryWrapperSupplierEV);
            Double shootLevel = 0D;
            Double postProduction = 0D;
            Double delivery2Level = 0D;
            Double delivery3Level = 0D;
            Double serviceAttitude = 0D;
            Double deliverySchedule = 0D;
            Double communicateSkill = 0D;
            int shootLevelCount = 0;
            int postProductionCount = 0;
            int delivery2LevelCount = 0;
            int delivery3LevelCount = 0;
            int serviceAttitudeCount = 0;
            int deliveryScheduleCount = 0;
            int communicateSkillCount = 0;
            for(SupplierEvaluates se : evList){
                Integer shootLevelItem = se.getShootLevel();
                if(shootLevelItem != -1){
                    shootLevel += shootLevelItem.doubleValue();
                    shootLevelCount ++;
                }
                Integer postProductionItem = se.getPostProduction();
                if(postProductionItem != -1){
                    postProduction += postProductionItem.doubleValue();
                    postProductionCount ++;
                }
                Integer delivery2LevelItem = se.getDelivery2Level();
                if(delivery2LevelItem != -1){
                    delivery2Level += delivery2LevelItem.doubleValue();
                    delivery2LevelCount ++;
                }
                Integer delivery3LevelItem = se.getDelivery3Level();
                if(delivery3LevelItem != -1){
                    delivery3Level += delivery3LevelItem.doubleValue();
                    delivery3LevelCount ++;
                }
                Integer serviceAttitudeItem = se.getServiceAttitude();
                if(serviceAttitudeItem != -1){
                    serviceAttitude += serviceAttitudeItem.doubleValue();
                    serviceAttitudeCount ++;
                }
                Integer deliveryScheduleItem = se.getDeliverySchedule();
                if(deliveryScheduleItem != -1){
                    deliverySchedule += deliveryScheduleItem.doubleValue();
                    deliveryScheduleCount ++;
                }
                Integer communicateSkillItem = se.getCommunicateSkill();
                if(communicateSkillItem != -1){
                    communicateSkill += communicateSkillItem.doubleValue();
                    communicateSkillCount ++;
                }
            }
            shootLevel = shootLevel / shootLevelCount ;
            postProduction = postProduction / postProductionCount ;
            delivery2Level = delivery2Level / delivery2LevelCount ;
            delivery3Level =delivery3Level / delivery3LevelCount ;
            serviceAttitude =serviceAttitude / serviceAttitudeCount ;
            deliverySchedule = deliverySchedule / deliveryScheduleCount ;
            communicateSkill =communicateSkill / communicateSkillCount ;
            Double evSum = (shootLevel + postProduction + delivery2Level + delivery3Level + serviceAttitude + deliverySchedule + communicateSkill)/ 7;
            supplier.setFinalEvaluation(evSum);
            supplier.setId(item);
            suppliers.add(supplier);
        }
        //3将综合评分更新至supplier表
        for(Supplier supplier : suppliers){
            UpdateWrapper<Supplier> supplierQueryWrapper = new UpdateWrapper<Supplier>();
            supplierQueryWrapper.eq("id",supplier.getId() );
            int boo = supplierDao.update(supplier, supplierQueryWrapper);
        }
        return Result.success();
    }
}
